package com.meixiang.activity.account.myShopper;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.meixiang.R;
import com.meixiang.activity.moments.ContentEditingActivity;
import com.meixiang.global.GlobalType;
import com.meixiang.main.BaseActivity;
import com.meixiang.main.MXApplication;
import com.meixiang.util.AbStrUtil;
import com.meixiang.util.GlideHelper;
import com.meixiang.view.photoSelectActivity.util.Bimp;
import com.meixiang.view.photoSelectActivity.util.ImageItem;
import com.meixiang.view.photoSelectActivity.zoom.PhotoView;
import com.meixiang.view.photoSelectActivity.zoom.ViewPagerFixed;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：黑哥 on 2016/5/14 15:40
 * <p/>
 * 图片列表
 */
public class PhotoListShowEvaluateActivity extends BaseActivity {
    @Bind(R.id.tv_title)
    TextView mTvTitle;
    @Bind(R.id.iv_delete)
    ImageView mIvDelete;
    @Bind(R.id.viewPager)
    ViewPagerFixed mViewPager;
    @Bind(R.id.tv_photo_number_show)
    TextView mTvPhotoNumberShow;
    @Bind(R.id.tl_tab)
    RelativeLayout mTlTab;
    @Bind(R.id.tv_finish)
    TextView mTvFinish;
    private Activity mActivity;

    private int position,orderPosition;
    //当前的位置
    private int location = 0;
    private ArrayList<View> listViews = null;
    private MyPageAdapter adapter;
    private String title;
    private List<String>  imagePath;

    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_photo_list_show);
        mActivity = PhotoListShowEvaluateActivity.this;
        MXApplication.mApp.AddActivity(mActivity);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }


    @Override
    protected void initTitle() {
        title=getIntent().getStringExtra("title");
        position=getIntent().getIntExtra("position",0);
        imagePath=getIntent().getStringArrayListExtra("imagePath");
        orderPosition=getIntent().getIntExtra("orderPosition",0);
        if(!AbStrUtil.isEmpty(title)){
            mTvTitle.setText(title);
        }
    }

    @Override
    protected void initView() {
        mViewPager.addOnPageChangeListener(pageChangeListener);
        for (int i = 0; i < imagePath.size(); i++) {
            initListViews(imagePath.get(i));
        }
        adapter = new MyPageAdapter(listViews);
        mViewPager.setAdapter(adapter);
        mViewPager.setCurrentItem(position);
        mViewPager.setPageMargin((int) getResources().getDimensionPixelOffset(R.dimen.divide_10_dp));
        mTvFinish.setOnClickListener(this);
        mIvDelete.setOnClickListener(this);


    }

    @Override
    protected void setData() {
        if (imagePath.size() > 0) {
            mTvPhotoNumberShow.setText("("+(position+1)+"/"+imagePath.size()+")");
        }
    }

    @Override
    public void onClick(View v) {
        if (v.equals(mTvFinish)) {
            this.finish();
        }if (v.equals(mIvDelete)){

            if (listViews.size() == 1) {
                imagePath.clear();
            } else {
                imagePath.remove(location);
                mViewPager.removeAllViews();
                listViews.remove(location);
                adapter.setListViews(listViews);
            }
            adapter.notifyDataSetChanged();
            Intent intent = new Intent(GlobalType.EVALUATE_ORDER_DELETE_PHOTO);
            intent.putExtra("position",location);
            intent.putExtra("orderPosition",orderPosition);
            Log.e("删除照片","删除position==="+location);
            mTvPhotoNumberShow.setText("(" +(location+1) + "/"+imagePath.size()+")");
            sendBroadcast(intent);
            if(imagePath.size()==0){
                this.finish();
            }
        }
    }

    private void initListViews(String url) {
        if (listViews == null)
            listViews = new ArrayList<View>();
        PhotoView img = new PhotoView(this);
        img.setBackgroundColor(0xff000000);
//        try {
//            Bitmap bitmap = Bimp.revitionImageSize(url);
//            if(bitmap!=null){
//                img.setImageBitmap(bitmap);
//                listViews.add(img);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        img.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        Glide.with(context)
                .load(url)
                .placeholder(R.drawable.image_default_gray_bg)
                .error(R.drawable.image_default_gray_bg)
                .into(img);
        listViews.add(img);
//        GlideHelper.showImage(context,url,img);
    }

    private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {

        public void onPageSelected(int arg0) {
            location = arg0;
            mTvPhotoNumberShow.setText("(" + (arg0+1) + "/"+imagePath.size()+")");
        }

        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        public void onPageScrollStateChanged(int arg0) {

        }
    };

    class MyPageAdapter extends PagerAdapter {

        private ArrayList<View> listViews;

        private int size;

        public MyPageAdapter(ArrayList<View> listViews) {
            this.listViews = listViews;
            size = listViews == null ? 0 : listViews.size();
        }

        public void setListViews(ArrayList<View> listViews) {
            this.listViews = listViews;
            size = listViews == null ? 0 : listViews.size();
        }

        public int getCount() {
            return size;
        }

        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

//        public void destroyItem(View arg0, int arg1, Object arg2) {
//            ((ViewPagerFixed) arg0).removeView(listViews.get(arg1 % size));
//        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(listViews.get(position % size));
        }

        //        @Override
//        public void destroyItem(ViewGroup container, int position, Object object) {
//            ((ViewPagerFixed) container).removeView(listViews.get(position % size));
//        }

//        public void finishUpdate(View arg0) {
//        }

        @Override
        public void finishUpdate(ViewGroup container) {
            super.finishUpdate(container);
        }




        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(listViews.get(position % size), 0);
            return listViews.get(position % size);
        }

        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

    }

    /**
     * 监听返回按钮
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
           this.finish();
        }
        return true;
    }
}
